﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using HU8.Helpers.Files;

namespace HU8.Web.FileExplorer.FileExplorer.UserControls
{
    public partial class ContentList : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void LoadContentList(string relativePath, string physicalPath)
        {
            string html = "";
            string path = physicalPath;
            string absolutePath = VirtualPathUtility.ToAbsolute(relativePath);
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            DirectoryInfo[] directoryInfoList = directoryInfo.GetDirectories();
            foreach (DirectoryInfo di in directoryInfoList)
            {
                string size = FileHelper.SizeToString(FileHelper.GetDirectorySize(di.FullName));
                html += string.Format("<tr><td><input class='fe-select-path' type='checkbox' value='{0}'/></td><td><a href='javascript:void(0)' class='dir cmd-loadinfo' rel='{0}'>{1}</a></td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>", relativePath + di.Name, di.Name, size, di.LastWriteTime, di.Extension, di.GetAccessControl().ToString());
            }

            FileInfo[] fileInfoList = directoryInfo.GetFiles();
            foreach (FileInfo fi in fileInfoList)
            {
                string ext = "";
                try
                {
                    ext = fi.Extension.Substring(1);
                }
                catch (Exception ex)
                {

                }
                string size = FileHelper.SizeToString(fi.Length);
                html += string.Format("<tr><td><input class='fe-select-path' type='checkbox' value='{0}'/></td><td><a href='javascript:void(0)' class='file {2} cmd-loadinfo' rel='{0}'>{1}</a></td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>", relativePath + fi.Name, fi.Name, size, fi.LastWriteTime, ext, fi.GetAccessControl().ToString());
            }
            ltrListItem.Text = html;
        }

    }
}